#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "thread_ipc_shm_msg.h"

#define QUEUE_NAME "test"
#define QUEUE_NAME1 "test1"
#define QUEUE_NAME2 "test2"

static void *producer(void *arg)
{
    int count = 0;
    int queue_id = queue_create(QUEUE_NAME1, 64, 5);
    char buff[64];
    while (1)
    {
        memset(buff, 0x00, sizeof(buff));
        sprintf(buff, "hello %d", count++);
        sleep(rand() % 3);
        queue_send(queue_id, buff, sizeof(buff));
    }
}

static void *consumer(void *arg)
{
    int queue_id = queue_open(QUEUE_NAME1, 2000);
    char buff[64];
    while (1)
    {
        memset(buff, 0x00, sizeof(buff));
        queue_recv(queue_id, buff, sizeof(buff));
        printf("recv : %s\n", buff);
    }
}

static void *producer1(void *arg)
{
    int count = 0;
    int queue_id = queue_create(QUEUE_NAME2, 64, 5);
    char buff[64];
    while (1)
    {
        memset(buff, 0x00, sizeof(buff));
        sprintf(buff, "hello111 %d", count++);
        sleep(rand() % 3);
        queue_send(queue_id, buff, sizeof(buff));
    }
}

static void *consumer1(void *arg)
{
    int queue_id = queue_open(QUEUE_NAME2, 2000);
    char buff[64];
    while (1)
    {
        memset(buff, 0x00, sizeof(buff));
        queue_recv(queue_id, buff, sizeof(buff));
        printf("---------------recv : %s\n", buff);
    }
}

int main(void)
{
    srand(time(NULL));
    pthread_t th_a, th_b,th_c,th_d;
    void *retval;
    pthread_create(&th_a, NULL, producer, 0);
    pthread_create(&th_b, NULL, consumer, 0);
    pthread_create(&th_c, NULL, producer1, 0);
    pthread_create(&th_d, NULL, consumer1, 0);
    pthread_join(th_a, &retval);
    pthread_join(th_b, &retval);
    pthread_join(th_c, &retval);
    pthread_join(th_d, &retval);
    return 0;
}